import Vue from 'vue'
import VueRouter from 'vue-router'
import local from '@/utils/local'
import { Message } from 'element-ui'; //引入全局的Message
Vue.use(VueRouter)
const users=[
  {
    path: '/',
    redirect:'/login'
  },
// 登录
{
  path: '/login',
  component: () => import('@/views/login/Login.vue'),

  meta: {
    title: '登录-沐麟外卖管理系统' // 修改页面标题
  },

},
// 后台首页
{
  path: '/index',
  component: () => import('@/views/index/Layout.vue'),
  meta:{title:'后台首页'},
  isMenu:true,//声明要做导航的路由
  icon:'el-icon-s-home',
  children:[
    { path: '/', component: () => import('@/views/index/Index.vue'),}
  ]
},
  // 商品管理
  {
    path: '/goods',
    meta:{title:'商品管理'},
    isMenu:true,//声明要做导航的路由
    icon:'el-icon-s-goods',
    component: () => import('@/views/index/Layout.vue'),
    redirect:'/goods/list',
    children:[
      {
        path: '/goods/list', 
        meta:{title:'商品列表'},
        isMenu:true,//声明要做导航的路由
        component: () => import('@/views/goods/GoodList.vue'),
      },
      {
        path: '/goods/add', 
        meta:{title:'添加商品'},
        isMenu:true,//声明要做导航的路由
        component: () => import('@/views/goods/GoodsAdd.vue'),
      },
      {
        path: '/goods/category',
        meta:{title:'商品分类'}, 
        isMenu:true,//声明要做导航的路由
        component: () => import('@/views/goods/GoodsCategory.vue'),
      },
      {
        path: '/goods/edit',
        meta:{title:'修改商品'}, 
        component: () => import('@/views/goods/GoodsEdit.vue'),
      },
    ]
  },
  // 订单管理
{
  path: '/order',
  meta:{title:'订单管理'},
  isMenu:true,//声明要做导航的路由
  icon:'el-icon-s-order',
  component: () => import('@/views/index/Layout.vue'),
  redirect:'/order',
  children:[
    {
      path: '/order', 
      component: () => import('@/views/order/order.vue'),
    },
  ]
},  //账号管理
{
  path: '/account',
  component: () => import('@/views/index/Layout.vue'),
  meta:{title:'账号管理'},
  redirect:'/account/list',
  children:[
    {
      path: '/account/personaldata',
      meta:{title:'管理员信息'}, 
      component: () => import('@/views/account/PersonalData.vue'),
    },
  ]
},
];

// 超级管理员权限
const admins = [

  //账号管理
  {
    path: '/account',
    component: () => import('@/views/index/Layout.vue'),
    meta:{title:'账号管理'},
    isMenu:true,//声明要做导航的路由
    icon:'el-icon-user-solid',
    redirect:'/account/list',
    children:[
      {
        path: '/account/list', 
        meta:{title:'账号列表'},
        isMenu:true,//声明要做导航的路由
        component: () => import('@/views/account/AccountList.vue'),
      },
      {
        path: '/account/add', 
        meta:{title:'添加账号'},
        isMenu:true,//声明要做导航的路由
        component: () => import('@/views/account/AccountAdd.vue'),
      },
      {
        path: '/account/changepassword', 
        meta:{title:'修改密码'},
        isMenu:true,//声明要做导航的路由
        component: () => import('@/views/account/ChangePassword.vue'),
      },
      {
        path: '/account/personaldata',
        meta:{title:'管理员信息'}, 
        component: () => import('@/views/account/PersonalData.vue'),
      },
    ]
  },

  

  // 销售统计
  {
    path: '/count',
    meta:{title:'销售统计'},
    isMenu:true,//声明要做导航的路由
    icon:'el-icon-pie-chart',
    component: () => import('@/views/index/Layout.vue'),
    redirect:'/count/goods',
    children:[
      {
        path: '/count/goods', 
        meta:{title:'商品统计'},
        isMenu:true,//声明要做导航的路由
        component: () => import('@/views/count/CountGoods.vue'),
      },
      {
        path: '/count/order', 
        meta:{title:'订单统计'},
        isMenu:true,//声明要做导航的路由
        component: () => import('@/views/count/CountOrder.vue'),
      },
    ]
  },
  // 店铺管理
  {
    path: '/shop',
    meta:{title:'店铺管理'},
    icon:'el-icon-s-shop',
    isMenu:true,//声明要做导航的路由
    component: () => import('@/views/index/Layout.vue'),
    children:[
      {
        path: '', 
        component: () => import('@/views/shop/Shop.vue'),
      },
    ]
  },


]
// 防止访问重复页面时报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
// 定义一个函数,判断角色返回不同的路由
//users 普通用户
//超级管理员
export  function showRouter(){
  let role =local.get('role')
  if(role==='super'){
    return [...users,...admins]
  } else{
    return users;
  }
}
// 路由守卫
const router = new VueRouter({
  routes:showRouter()
});


router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title
  } else {
    document.title = '沐麟外卖管理系统' // 默认标题
  }
  if(to.path === '/login'){
    next()
  }
  else{
    let token = local.get('token')
    if(token){
      next()
    }
    else{
      Message.error("未登录！请登录");
      next({path:'/login'})
    }
  }


})
export default router
